#!/bin/sh

test_description="test proc-receive"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
  REPO_ID=$(find_repo | jq .id)
  if [ -n $REPO_ID ]; then
    delete_repo > /dev/null 2>&1
    test_expect_code 0
  fi

  REPO_ID=$(create_repo) &&
  add_member $WRITE_LEVEL $another_user_external &&
  init_repo
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
  echo "This is the first commit" > "$REPO_DIR"/README &&
  git -C "$REPO_DIR" add README &&
  git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master > test-2 2>&1
'

#read权限普通push，失败
test_expect_success "[test-3][no][protected][read] push to protected branch fail" '
  protect_branch master &&
  add_member $READ_LEVEL $another_user_external &&
  echo "[test-8][no][protected][read] push to protected branch fail" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "push to protected branch fail" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REPO_DIR" push origin master -o review=no > test-3 2>&1 &&
  grep "failed to push some refs" test-3
'

#write权限push保护分支，失败
test_expect_success "[test-4][no][protected][write] push to protected branch depend on protected rules" '
  add_member $WRITE_LEVEL $another_user_external &&
  echo "[test-9][no][protected][write] push to protected branch depend on protected rules" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "push to protected branch depend on protected rules" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REPO_DIR" push origin master -o review=no > test-4 2>&1 &&
  grep "failed to push some refs" test-4
'

#read权限push非protected分支，失败
test_expect_success "[test-5][no][non-protected][read] push to non-protected branch fail" '
  branch_id=$(get_protected_branch | jq '.[0].id') &&
  unprotect_branch $branch_id  &&
  add_member $READ_LEVEL $another_user_external &&
  echo "[test-10][no][non-protected][read] push to non-protected branch fail" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "push to non-protected branch fail" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REPO_DIR" push origin master -o review=no > test-5 2>&1 &&
  grep "failed to push some refs" test-5
'

#write权限push非protected分支，成功
test_expect_success "[test-6][no][non-protected][write] push to non-protected branch success" '
  add_member $WRITE_LEVEL $another_user_external &&
  echo "[test-11][no][non-protected][write] push to non-protected branch success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "push to non-protected branch success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=no > test-6 2>&1
'

test_expect_success "[test-7] clean up" '
  delete_repo "$REPO_ID" > test-7 2>&1
'

test_done